package com.software.modelcenter.controller;

import com.software.modelcenter.domain.Result;
import com.software.modelcenter.entity.ModelType;
import com.software.modelcenter.service.ModelTypeService;
import com.software.modelcenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @Author xiaoliao
 * @Date 2023/4/26 14:27
 */
@RestController
@RequestMapping("/model_type")
@RefreshScope
public class ModelTypeController {

    @Autowired
    private ModelTypeService modelTypeService;

    @GetMapping("/list")
    public List<ModelType> findAll() {
        List<ModelType> modelTypes = modelTypeService.findAll();
        return modelTypes;
    }

    @GetMapping("/list_page")
    public Page<ModelType> findAll(
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "5") String pageSize
    ) {
        Page<ModelType> page = modelTypeService.findAll(Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    @PostMapping("/addModelType")
    public Result<ModelType> addModelType(
            @RequestBody ModelType modelType){
        modelType.setTypeId(UUID.randomUUID().toString());
        ModelType modelType1 = modelTypeService.insertModelType(modelType);
        return ResultUtil.success(modelType1);
    }

    //通过typeID删除
    @GetMapping(value = "/deleteModelTypeByTypeId/{typeId}")
    public Result<ModelType> deleteModelTypeByTypeId(@PathVariable String typeId){
        modelTypeService.deleteModelTypeByTypeId(typeId);
        return ResultUtil.success(null);
    }

    //修改分类信息
    @PostMapping(value = "/updateModelType")
    public Result<ModelType> updateModelType(@RequestBody ModelType modelType){
        modelTypeService.updateModelType(modelType);
        return ResultUtil.success(modelType);
    }



    @GetMapping("/findById/{typeId}")
    public ModelType findById(@PathVariable String typeId) {
        return modelTypeService.findById(typeId);
    }

//    @GetMapping("/findByTypeDescriptionLike/{description}")
//    public List<ModelType> findByTypeDescriptionLike(@PathVariable String description) {
//        // 一定要加 "%"+参数名+"%"
//        return modelTypeService.findByTypeDescriptionLike("%"+description+"%");
//    }
//
//    @GetMapping("/findByTypeDescriptionAndTypeNameLike/{description}")
//    public List<ModelType> findByTypeDescriptionAndTypeNameLike(@PathVariable String description) {
//        // 一定要加 "%"+参数名+"%"
//        return modelTypeService.findByTypeDescriptionLikeOrTypeNameLike("%"+description+"%");
//    }

}
